package com.ctsi.ssdc.controller;

import com.ctsi.ssdc.entity.Excel12;
import com.ctsi.ssdc.entity.FixedInvestment;
import com.ctsi.ssdc.service.Excel12Service;
import com.ctsi.ssdc.service.ExcelService;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 12基础表投资（房地产）(Excel12)表控制层
 *
 * @author makejava
 * @since 2023-02-21 19:48:12
 */
@RestController
@RequestMapping("excel/12")
public class Excel12Controller {
    /**
     * 服务对象
     */
    @Resource
    private Excel12Service excel12Service;
    @Resource
    private ExcelService excelService;

    /**
     * 分页查询
     *
     * @param excel12 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Excel12>> queryByPage(Excel12 excel12,@PageableDefault Pageable pageRequest) {
        return ResponseEntity.ok(this.excel12Service.queryByPage(excel12,(PageRequest) pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Excel12> queryById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(this.excel12Service.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param excel12 实体
     * @return 新增结果
     */
    @PostMapping
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<Excel12> add(Excel12 excel12) {
        Excel12 insert = this.excel12Service.insert(excel12);
        excelService.sheet12Cal(excel12.getStatisticalTime());
        return ResponseEntity.ok(insert);
    }

    /**
     * 编辑数据
     *
     * @param excel12 实体
     * @return 编辑结果
     */
    @PutMapping
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<Excel12> edit(Excel12 excel12) {
        Excel12 update = this.excel12Service.update(excel12);
        excelService.sheet12Cal(excel12.getStatisticalTime());
        return ResponseEntity.ok(update);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<Boolean> deleteById(Long id) {
        Excel12 excel12 = excel12Service.queryById(id);
        boolean res = this.excel12Service.deleteById(id);
        excelService.sheet12Cal(excel12.getStatisticalTime());
        return ResponseEntity.ok(res);
    }

    /**
     * 房地产投资当月 + 累计 + 累计同比
     * @param year
     * @param month
     * @return
     */
    @ApiOperation("房地产投资当月 + 累计 + 累计同比")
    @GetMapping("realEstateInvestmentInfo/{year}/{month}")
    public ResponseEntity<FixedInvestment> getRealEstateInvestmentInfo(@PathVariable("year") String year,
                                                                       @PathVariable("month") String month) {
        return ResponseEntity.ok(excel12Service.getRealEstateInvestmentInfo(year,month));
    }

}

